<template>
  <a-modal
    title="新建"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="modalOk"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="客户">
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入客户名称'}], initialValue: model.name}]" />
        </a-form-item>
        <a-form-item label="座机">
          <a-input v-decorator="['telephone', {rules: [{required: false, message: '请输入客户座机'}], initialValue: model.telephone}]" />
        </a-form-item>
        <a-form-item label="手机号">
          <a-input v-decorator="['cellPhone', {rules: [{required: true, message: '请输入手机号'}], initialValue: model.cellPhone}]" />
        </a-form-item>
        <a-form-item label="地址">
          <a-input v-decorator="['address', {rules: [{required: true, message: '请输入地址'}], initialValue: model.address}]" />
        </a-form-item>
        <a-form-item label="详细地址">
          <a-input v-decorator="['addressDetail', {rules: [{required: true, message: '请输入详细地址'}], initialValue: model.addressDetail}]" />
        </a-form-item>
        <a-form-item label="客户类型">
          <a-input v-decorator="['customerType', {rules: [{required: true, message: '请输入客户类型'}], initialValue: model.customerType}]" />
        </a-form-item>
        <a-form-item label="经度">
          <a-input v-decorator="['lgt', {rules: [{required: true, message: '请输入经度'}], initialValue: model.lgt}]" />
        </a-form-item>
        <a-form-item label="纬度">
          <a-input v-decorator="['lat', {rules: [{required: true, message: '请输入纬度'}], initialValue: model.lat}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addCustomer, putCustomer } from '@/api/manage'
const fields = ['id']

export default {
  name: 'MemberForm',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    const _self = this
    this.$watch('model', () => {
      this.form = this.$form.createForm(this)
      _self.$nextTick(() => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    })
  },
  methods: {
    modalOk () {
      this.form.validateFieldsAndScroll((errors, values) => {
        if (!errors) {
          delete values.id
          if (this.model && this.model.id) {
            putCustomer(this.model.id, values).then(res => {
              this.$emit('ok')
            })
          } else {
            addCustomer(values).then(res => {
              this.$emit('ok')
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
